package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import controller.UI;

/**
 * Class is responsible for window where user enters the new logger depth he want to see.
 */
public class LoggerDepthFrame extends JFrame
{
	private static IntTextField intFiled;
	  
	/* Current shown window */
	public static LoggerDepthFrame currentLoggerDepthFrame = null;
	
	/**
	 * C'tor
	 * Define the frame
	 */
	public LoggerDepthFrame()
	{
		/* Window's characteristics */
		setTitle("Logger depth");
	    setSize(300, 100);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());

		/* Label */
		JLabel label = new JLabel("Enter the logger history depth (natural number):");
	    add(label, BorderLayout.NORTH);
	   
		/* Text for inserting integer */
	    intFiled = new IntTextField("", 3);
	    add(intFiled, BorderLayout.CENTER);

		/* Standart buttons */
	    addStandartButtons();
	    
	    /* Store the current open window */
	    currentLoggerDepthFrame = this;
	  }

	/**
	 * This method adds standart finish/cancel buttons to the current frame
	 * @param N/A
	 */
	private void addStandartButtons() 
	{
		Box tableBox = Box.createHorizontalBox();
		
		JButton finish = new JButton("Finish");
		finish.setToolTipText("Close and save changes");
		JButton cancel = new JButton("Cancel");
		cancel.setToolTipText("Close without saving changes");
		
		tableBox.add(finish);
		tableBox.add(cancel);
		
	    add(tableBox, BorderLayout.SOUTH);
	    
		/*Finish button listener - closes the frame and stores the changes */
		finish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if (intFiled.isValid())
				{
					Settings.historyDepth = intFiled.getValue();
					currentLoggerDepthFrame.dispose();
					
					LoggerTable.loggerData = UI.getLoggerSessionsReports(Settings.historyDepth);					
					LoggerTable.currentLoggerTable.build();
					StatisticsTable.currentStatisticsTable.build();
				}
			}	  
		});
		  
		/*Cancel button listener - closes the frame*/
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				currentLoggerDepthFrame.dispose();		
			}	  
		});
	}
}

/**
 * Class is responsible for text field where logger depth will be entered.
 */
class IntTextField extends JTextField
{
	/**
	 * C'tor
	 * @param defval - defult value for the text field
	 * @param size - the text field size
	 */
	public IntTextField(String defval, int size)
	{
		super("" + defval, size);
	}
 
	/**
	 * This method checks that the natural number value was entered to the text field.
	 * @param N/A
	 * @return 	true - if text in the text field is the natural number
	 * 			false - in other case
	 */
	public boolean isValid()
	{
		try
		{
			Integer.parseInt(getText());
			return true;
		} catch (NumberFormatException e) {
	    	//TODO: error msg
	    	System.out.println("Error");
	    	return false;
	    }
	  }

	/**
	 * This method gets the natural number value that was entered to the text field.
	 * @param N/A
	 * @return the natural number value that was entered to the text field.
	 * 			or the default value if theileegal string was entered to the text field
	 */
	  public int getValue() {
	    try {
	      return Integer.parseInt(getText());
	    } catch (NumberFormatException e) {
	    	//TODO: error msg
	    	System.out.println("Error");
	      return Settings.historyDepth;
	    }
	  }
}
